<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
        
      <meta name="description" content="欢迎与我交流自动化测试、测试开发、持续集成、DevOps方面知识经验，可在评论区留言。" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>使用pyecharts绘制系统依赖关系图 |  恰得福来的博客</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="恰得福来的博客" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-使用pyecharts绘制系统依赖关系图"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  使用pyecharts绘制系统依赖关系图
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/09/05/%E4%BD%BF%E7%94%A8pyecharts%E7%BB%98%E5%88%B6%E7%B3%BB%E7%BB%9F%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%E5%9B%BE/" class="article-date">
  <time datetime="2021-09-05T03:10:00.000Z" itemprop="datePublished">2021-09-05</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/python/">python</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.4k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">10 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="背景介绍"><a href="#背景介绍" class="headerlink" title="背景介绍"></a>背景介绍</h2><p>近期梳理了公司内部系统之间的数据关系，得到了多个excel格式的统计文件，每个文件包含了该系统自身数据清单、依赖的其他系统的数据清单、对其他系统供应的数据清单。<br>各系统之间依赖关系复杂，所以我想使用一些绘图工具，以可视化图形图表的形式展现这些系统之间的数据关系，给观众形成一种非常直观的印象。</p>
<p>一般情况下，应该是有类似于调用链跟踪等工具，比如Java技术栈使用Skywalking或美团CAT等工具，做到调用级别上的跟踪和相互关系的可视化展示。<br>这种情况下，也许不需要再编写程序来绘制依赖关系了。这两种工具可以很详细的绘制组件或服务之间的调用关系。</p>
<p>不过，本文介绍如何绘制粒度稍微粗一些的关系图，没有细化到具体某个接口调用。主要的工具是pyecharts，百度开源的工具包。在绘制的时候，只使用了依赖表和对外供应两张表。</p>
<h2 id="可视化思路"><a href="#可视化思路" class="headerlink" title="可视化思路"></a>可视化思路</h2><p>可视化的方式有很多种，我的需求是以最小成本，最快速度把这个可视化效果达成。不管是使用类似思维导图或draw.io这种工具，或者是编码实现，只要可以实现，任何方式都可以考虑。</p>
<p>我考虑的第一种方式，是使用draw.io。它确实提供了很多模型，但是最终我发现，手动去绘图，会花很多时间，然后效果还一般。<br>大概花了总共2小时的时间，发现用draw.io来绘制十多个系统之间复杂的连接，还是有点难。</p>
<p>所以，我考虑第二种方式是使用思维导图。确实思维导图很快可以把图建立起来，因为执行一次<code>回车</code>操作就可以生成一个方框，速度非常快。但是我嫌这个图不好看。<br>还没等我完成全部的系统和系统之间的连线，我就不想再做下去了。</p>
<p>最后，我决定使用编码的方式实现。这样的好处有两点：第一是生成的图形会很好看，效果好，第二是后续的维护会自动化，一劳永逸。</p>
<h2 id="实现可视化"><a href="#实现可视化" class="headerlink" title="实现可视化"></a>实现可视化</h2><p>经过调研了几个框架，包括pyecharts，politly，最终我发现pyecharts的关系图其实可以实现我的想法。</p>
<p>我基于<a target="_blank" rel="noopener" href="https://gallery.pyecharts.org/#/Graph/graph_les_miserables">https://gallery.pyecharts.org/#/Graph/graph_les_miserables</a>这个案例实现了想要的效果。</p>
<p>效果图如下，使用菱形图表示系统，圆形便是系统的数据：<br><img src="/images/result1.png" alt="效果图"></p>
<h2 id="编码实现"><a href="#编码实现" class="headerlink" title="编码实现"></a>编码实现</h2><p>相关的代码或文件分三部分，第一部分是包含系统关系的原始文件，放在files_excel目录下，第二部分是用于生成表示图形信息的json文件的python脚本<code>build_json_system_relation.py</code>，第三部分是绘制图形的代码文件<code>generate_relations.py</code>。</p>
<h3 id="数据关系文件"><a href="#数据关系文件" class="headerlink" title="数据关系文件"></a>数据关系文件</h3><p>files_excel目录下的文件，每个文件包含两个sheet（表单），即<code>对外供应数据表清单</code>和<code>所依赖外部系统数据表清单</code>。</p>
<p><code>对外供应数据表清单</code>表单的格式如下：</p>
<table>
<thead>
<tr>
<th align="center">目的系统</th>
<th align="center">数据英文名</th>
<th align="center">数据中文名</th>
<th align="center">数据交换方式</th>
</tr>
</thead>
<tbody><tr>
<td align="center">B</td>
<td align="center">table1</td>
<td align="center">数据1</td>
<td align="center">交易线</td>
</tr>
<tr>
<td align="center">C</td>
<td align="center">table2</td>
<td align="center">数据2</td>
<td align="center">数据线</td>
</tr>
<tr>
<td align="center">D</td>
<td align="center">table3</td>
<td align="center">数据3</td>
<td align="center">数据线</td>
</tr>
</tbody></table>
<p><code>所依赖外部系统数据表清单</code>表单的格式与上面类似，不过第一列是源系统。</p>
<p>上述两个文件就是我们要使用到的系统数据关系文件。如果有工具可以产生上述文件，那所有的步骤都可以自动化完成，包括生成本文讲的这种简单的关系图。如果不能自动生成，那么可能需要人工维护。所以建议还是都能实现自动化。关于如何产生上述关系，不在本文范围内，不过我觉得这种关系在任何一家公司都会维护起来的，比如通过提供一个维护页面维护上述两张表。</p>
<h3 id="生成表示图形中节点和边的JSON文件"><a href="#生成表示图形中节点和边的JSON文件" class="headerlink" title="生成表示图形中节点和边的JSON文件"></a>生成表示图形中节点和边的JSON文件</h3><p>现在我们已经有了原始关系文件，这种文件一般是for人类的，就是说给人看和维护的。那么现在我们需要把它们转成给代码使用的json文件。</p>
<p>json文件格式大致如下，可以先了解一下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;categories&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;data node&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;symbol&quot;</span>: <span class="string">&quot;circle&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;name&quot;</span>: <span class="string">&quot;system node&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;symbol&quot;</span>: <span class="string">&quot;diamond&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">&quot;nodes&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;label&quot;</span>: <span class="string">&quot;SYSTEM_A&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;id&quot;</span>: <span class="string">&quot;SYSTEM_A&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;category&quot;</span>: <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;label&quot;</span>: <span class="string">&quot;SYSTEM_B&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;id&quot;</span>: <span class="string">&quot;SYSTEM_B&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;category&quot;</span>: <span class="number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;label&quot;</span>: <span class="string">&quot;SYSTEM_C&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;id&quot;</span>: <span class="string">&quot;SYSTEM_C&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;category&quot;</span>: <span class="number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;label&quot;</span>: <span class="string">&quot;SYSTEM_A#table1&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;id&quot;</span>: <span class="string">&quot;SYSTEM_A#table1&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;category&quot;</span>: <span class="number">0</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">&quot;edges&quot;</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;sourceID&quot;</span>: <span class="string">&quot;SYSTEM_A#table1&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;attributes&quot;</span>: &#123;&#125;,</span><br><span class="line">      <span class="attr">&quot;targetID&quot;</span>: <span class="string">&quot;SYSTEM_B&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;size&quot;</span>: <span class="number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">&quot;sourceID&quot;</span>: <span class="string">&quot;SYSTEM_A#table1&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;attributes&quot;</span>: &#123;&#125;,</span><br><span class="line">      <span class="attr">&quot;targetID&quot;</span>: <span class="string">&quot;SYSTEM_C&quot;</span>,</span><br><span class="line">      <span class="attr">&quot;size&quot;</span>: <span class="number">1</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上述文件包含了三部分，第一份是<code>categories</code>，表示分类，可以将节点进行分类，方便区分；第二部分是<code>nodes</code>，表示图中所有节点，第三部分是<code>edges</code>表示图中所有边。</p>
<p>生成上述JSON文件的大概思路或算法是这样的：使用pandas读取excel表的数据，第一列（源或目的系统）作为系统节点，而第三列也就是数据列作为数据节点，对应JSON文件中的<code>nodes</code>中的每个元素。每一行都是一个关系，作为一条边，源节点是数据节点，目的节点是系统节点，对应JSON文件中的<code>edges</code>中的每个元素。通过遍历所有excel文件中的两个表单的每一行（非空），建立节点、边的信息，形成一个大的JSON文件。</p>
<p>关键代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_all_node_data</span>(<span class="params">data_dir</span>):</span></span><br><span class="line">    all_node_data = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">import</span> os</span><br><span class="line">    <span class="keyword">for</span> root, dirs, files <span class="keyword">in</span> os.walk(data_dir):</span><br><span class="line">        <span class="keyword">for</span> file <span class="keyword">in</span> files:</span><br><span class="line">            <span class="comment"># get node data</span></span><br><span class="line">            filepath = os.path.join(root, file)</span><br><span class="line">            all_node_data.extend(read_data_from_excel(filepath))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> all_node_data</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">read_data_from_excel</span>(<span class="params">filename</span>):</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;从excel读取数据，数据项作为一个节点（信息类）&quot;&quot;&quot;</span></span><br><span class="line">    out = []</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 系统节点</span></span><br><span class="line">    system_name = get_system_name(filename)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 读取&quot;对外供应数据表清单&quot;</span></span><br><span class="line">    <span class="comment"># 目的系统名称 表英文名 表中文名称 数据供应方式（交易线，数据线）</span></span><br><span class="line">    data = pd.read_excel(filename, sheet_name=<span class="string">&quot;对外供应数据表清单&quot;</span>, engine=<span class="string">&quot;openpyxl&quot;</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(data)):</span><br><span class="line">        row = data.iloc[i]</span><br><span class="line">        info_name = system_name + <span class="string">&quot;#&quot;</span> + trim_node_name(row[<span class="number">2</span>])  <span class="comment"># 类似 A#财务数据 这种格式</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># 处理以/分隔的多个系统</span></span><br><span class="line">        system_target_list = []</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&quot;/&quot;</span> <span class="keyword">in</span> row[<span class="number">0</span>]:</span><br><span class="line">            systems = <span class="built_in">str</span>.split(row[<span class="number">0</span>], <span class="string">&quot;/&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> system <span class="keyword">in</span> systems:</span><br><span class="line">                system_target = get_system_name(system)</span><br><span class="line">                <span class="keyword">if</span> system_target <span class="keyword">not</span> <span class="keyword">in</span> SYSTEM_IGNORE:</span><br><span class="line">                    system_target_list.append(system_target)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            system_target = get_system_name(row[<span class="number">0</span>])</span><br><span class="line">            <span class="keyword">if</span> system_target <span class="keyword">not</span> <span class="keyword">in</span> SYSTEM_IGNORE:</span><br><span class="line">                system_target_list.append(system_target)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> system_target <span class="keyword">in</span> system_target_list:</span><br><span class="line">            tmp_node = copy.deepcopy(generate_node_data(info_name, system_target, row[<span class="number">1</span>], row[<span class="number">2</span>], row[<span class="number">3</span>]))</span><br><span class="line">            out.append(tmp_node)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 读取&quot;所依赖外部系统数据表清单&quot;</span></span><br><span class="line">    <span class="comment"># 目的系统名称 表英文名 表中文名 数据供应方式（交易线，数据线）</span></span><br><span class="line">    data2 = pd.read_excel(filename, sheet_name=<span class="string">&quot;所依赖外部系统数据表清单&quot;</span>, engine=<span class="string">&quot;openpyxl&quot;</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(data2)):</span><br><span class="line">        row = data2.iloc[i]</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 处理以/分隔的多个系统</span></span><br><span class="line">        system_src_list = []</span><br><span class="line">        <span class="keyword">if</span> <span class="string">&quot;/&quot;</span> <span class="keyword">in</span> row[<span class="number">0</span>]:</span><br><span class="line">            systems_src = <span class="built_in">str</span>.split(row[<span class="number">0</span>], <span class="string">&quot;/&quot;</span>)</span><br><span class="line">            <span class="keyword">for</span> system <span class="keyword">in</span> systems_src:</span><br><span class="line">                system_src_list.append(system)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            system_src_list.append(row[<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line">        system_target = system_name</span><br><span class="line">        <span class="keyword">for</span> system <span class="keyword">in</span> system_src_list:</span><br><span class="line">            system_name_src = get_system_name(system)</span><br><span class="line">            <span class="keyword">if</span> system_name_src <span class="keyword">not</span> <span class="keyword">in</span> SYSTEM_IGNORE:</span><br><span class="line">                info_name = system_name_src + <span class="string">&quot;#&quot;</span> + trim_node_name(row[<span class="number">2</span>])</span><br><span class="line">                tmp_node = copy.deepcopy(generate_node_data(info_name, system_target, row[<span class="number">1</span>], row[<span class="number">2</span>], row[<span class="number">3</span>]))</span><br><span class="line">                out.append(tmp_node)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> out</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="使用pyecharts绘制图形"><a href="#使用pyecharts绘制图形" class="headerlink" title="使用pyecharts绘制图形"></a>使用pyecharts绘制图形</h3><p>现在已经有JSON文件了，工作完成了大部分。接下来就是编写代码绘制关系图，生成一个html文件。这个html文件是可交互的，可以移动鼠标或缩放。如果未来在这个页面增加其他一些检索或表格生成功能就更好了。可以作为未来继续尝试的点。这块没有什么算法逻辑。</p>
<p>关键代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">json_file = <span class="string">&quot;all_node_data.json&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(json_file, <span class="string">&#x27;r&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    data_local = f.read()</span><br><span class="line">    data = json.loads(data_local)</span><br><span class="line"></span><br><span class="line">nodes = [</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment"># &quot;x&quot;: node[&quot;x&quot;],</span></span><br><span class="line">        <span class="comment"># &quot;y&quot;: node[&quot;y&quot;],</span></span><br><span class="line">        <span class="string">&quot;id&quot;</span>: node[<span class="string">&quot;id&quot;</span>],</span><br><span class="line">        <span class="string">&quot;name&quot;</span>: node[<span class="string">&quot;label&quot;</span>],</span><br><span class="line">        <span class="string">&quot;symbolSize&quot;</span>: node[<span class="string">&quot;size&quot;</span>],</span><br><span class="line">        <span class="string">&quot;symbol&quot;</span>: node[<span class="string">&quot;symbol&quot;</span>],</span><br><span class="line">        <span class="comment"># &quot;itemStyle&quot;: &#123;&quot;normal&quot;: &#123;&quot;color&quot;: node[&quot;color&quot;]&#125;&#125;,</span></span><br><span class="line">        <span class="string">&quot;category&quot;</span>: node[<span class="string">&quot;category&quot;</span>]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> node <span class="keyword">in</span> data[<span class="string">&quot;nodes&quot;</span>]</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">edges = [</span><br><span class="line">    &#123;<span class="string">&quot;source&quot;</span>: edge[<span class="string">&quot;sourceID&quot;</span>], <span class="string">&quot;target&quot;</span>: edge[<span class="string">&quot;targetID&quot;</span>], <span class="string">&quot;value&quot;</span>: edge[<span class="string">&quot;value&quot;</span>],</span><br><span class="line">     <span class="string">&quot;linestyle_opts&quot;</span>: opts.LineStyleOpts(type_=edge[<span class="string">&quot;type_&quot;</span>]),</span><br><span class="line">     &#125; <span class="keyword">for</span></span><br><span class="line">    edge <span class="keyword">in</span> data[<span class="string">&quot;edges&quot;</span>]</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">categories = data[<span class="string">&quot;categories&quot;</span>]</span><br><span class="line"></span><br><span class="line">(</span><br><span class="line">    Graph(init_opts=opts.InitOpts(width=<span class="string">&quot;1600px&quot;</span>, height=<span class="string">&quot;1200px&quot;</span>))</span><br><span class="line">        .add(</span><br><span class="line">        series_name=<span class="string">&quot;&quot;</span>,</span><br><span class="line">        nodes=nodes,</span><br><span class="line">        links=edges,</span><br><span class="line">        categories=categories,</span><br><span class="line">        layout=<span class="string">&quot;circular&quot;</span>,  <span class="comment"># circular , force, none</span></span><br><span class="line">        is_roam=<span class="literal">True</span>,</span><br><span class="line">        is_focusnode=<span class="literal">True</span>,</span><br><span class="line">        repulsion=<span class="number">100</span>,</span><br><span class="line">        is_rotate_label=<span class="literal">True</span>,</span><br><span class="line">        label_opts=opts.LabelOpts(is_show=<span class="literal">True</span>, position=<span class="string">&quot;right&quot;</span>),</span><br><span class="line">        linestyle_opts=opts.LineStyleOpts(width=<span class="number">1</span>, curve=<span class="number">0.3</span>, opacity=<span class="number">1.0</span>),  <span class="comment"># type_=&quot;solid&quot;</span></span><br><span class="line">        edge_symbol=[<span class="string">&quot;&quot;</span>, <span class="string">&quot;arrow&quot;</span>],  <span class="comment"># 单向箭头</span></span><br><span class="line">        edge_label=opts.LabelOpts(</span><br><span class="line">            is_show=<span class="literal">False</span>, position=<span class="string">&quot;middle&quot;</span>, formatter=<span class="string">&quot;&#123;b&#125;: &#123;c&#125; &quot;</span>  <span class="comment"># 设置关系说明</span></span><br><span class="line">        )</span><br><span class="line">    )</span><br><span class="line">        .set_global_opts(title_opts=opts.TitleOpts(title=<span class="string">&quot;系统数据关系图&quot;</span>),</span><br><span class="line">                         legend_opts=opts.LegendOpts(orient=<span class="string">&quot;vertical&quot;</span>, pos_left=<span class="string">&quot;2%&quot;</span>, pos_top=<span class="string">&quot;20%&quot;</span>), )</span><br><span class="line">        .render(<span class="string">&quot;relations-systems.html&quot;</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>代码地址：<a target="_blank" rel="noopener" href="https://gitee.com/zackli518/charts_system_relations/tree/master">https://gitee.com/zackli518/charts_system_relations/tree/master</a></p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>参考：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://cloud.tencent.com/developer/article/1741359">https://cloud.tencent.com/developer/article/1741359</a></li>
<li><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/358471558">https://zhuanlan.zhihu.com/p/358471558</a> 了解了比较多类的属性</li>
<li><a target="_blank" rel="noopener" href="https://gallery.pyecharts.org/#/Graph/graph_les_miserables">https://gallery.pyecharts.org/#/Graph/graph_les_miserables</a> 官方文档说明，案例比较丰富</li>
<li><a target="_blank" rel="noopener" href="https://github.com/pyecharts/pyecharts-gallery">https://github.com/pyecharts/pyecharts-gallery</a> pyecharts的github地址</li>
</ul>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://johnny1952.github.io/2021/09/05/%E4%BD%BF%E7%94%A8pyecharts%E7%BB%98%E5%88%B6%E7%B3%BB%E7%BB%9F%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%E5%9B%BE/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/pyecharts/" rel="tag">pyecharts</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8F%AF%E8%A7%86%E5%8C%96/" rel="tag">可视化</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/09/08/%E8%B0%83%E8%AF%95JMeter5%E6%BA%90%E7%A0%81/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            调试JMeter5源码
          
        </div>
      </a>
    
    
      <a href="/2021/08/29/%E4%BD%BF%E7%94%A8JMeter5%E6%B5%8B%E8%AF%95PostgreSQL%E6%95%B0%E6%8D%AE%E5%BA%93/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">使用JMeter5测试PostgreSQL数据库</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "BDYyK8ADltP2ULGCnAJe5ufP-gzGzoHsz",
    app_key: "yCqG1hemYrFKJahvOBl6uppH",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "你有什么想法或问题？快来评论一下吧!",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021
        <i class="ri-heart-fill heart_icon"></i> Johnny Li
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1280251923&amp;web_id=1280251923'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="恰得福来的博客"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="https://johnny1952.github.io/gitbook-tutorial">gitbook</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>